// pages/coinCertificateOrder/coinCertificateOrder.js
import { shareOptions } from "../../utils/access";
import api from "../../config/api";
import request from "../../utils/request";
import { jump } from "../../utils/utils";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 筛选数据
    filterArray: [
      {label: '全部', value: 0},
      {label: '待发货', value: 1},
      {label: '已发货', value: 2},
    ],
    filterIndex: 1,
    orderList: [], //订单列表
    page: 1,
    total: 0,
    isDone: false,
    // 是否选中批量发货
    hadCheckedBatch: false,
    // 批量发货底部弹窗显示
    shipmentModalShow: false,
    // 底部安全距离
    safeBottom: 0,
    stepHeight: 217,
    bottomHeight: 48,
    selectdIdArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 适配iPhoneX系列--底部弹窗距离
    wx.getSystemInfo({
      success: (res) => {
        let safeBottom = res.screenHeight - res.safeArea.bottom
        this.setData({
          safeBottom,
          stepHeight: safeBottom + 217,
          bottomHeight: safeBottom + 48,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1,
      isDone: false,
      selectdIdArr: []
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
    this.getList(1)
  },
 
  /**
   * 获取订单列表
   */
  async getList(page){
    let status = this.data.filterArray[this.data.filterIndex].value
    let res = await request.get(api.expressOrderList, {
      status,
      page,
      size: 10
    }, { loading: false })
    if(res.httpStatus === 200 ){
      res?.data?.records.map((item)=>{
        item.selected = false
      })
      this.setData({
        orderList: page === 1 ? res?.data?.records : [
          ...this.data.orderList,
          ...res?.data?.records,
        ],
        total: res?.data?.pages
      })
    }
    wx.stopPullDownRefresh()
  },
  // 选择筛选
  bindPickerChange: function(e) {
    let { hadCheckedBatch } = this.data
    let flag = hadCheckedBatch
    if(hadCheckedBatch){
      if(Number(e.detail.value) === 1){
        return
      }else{
        flag = false
      }
    }
    this.setData({
      page: 1,
      isDone: false,
      filterIndex: e.detail.value,
      hadCheckedBatch: flag,
      shipmentModalShow: flag,
      selectdIdArr: [],
    })

    this.getList(1)
  },
  // 去发货、修改物流
  handle(e){
    let { item } = e.currentTarget.dataset
    jump('coinCertificateShipping',{
      id: item.id
    })
  },
  // 点击头部切换批量发货
  handleBatchShipment(){
    let { hadCheckedBatch } = this.data
    this.setData({
      page: 1,
      isDone: false,
      filterIndex: 1,
      hadCheckedBatch: !hadCheckedBatch,
      shipmentModalShow: !hadCheckedBatch,
      selectdIdArr: []
    })
    
    this.getList(1)
  },
  // 选择某项
  selectItem(e) {
    let { orderList } = this.data
    const index = e.currentTarget.dataset.index;
    const selected = orderList[index].selected;
    orderList[index].selected = !selected;
    let arr = []
    orderList.map((item)=>{
      if(item.selected){
        arr.push(item.id)
      }
    })
    this.setData({
      orderList,
      selectdIdArr: arr
    });
  },
  // 批量发货--取消选中
  handleCloseBatch(){
    let { orderList } = this.data
    orderList.map((item)=>{
      item.selected = false
    })
    this.setData({
      selectdIdArr: [],
      orderList: orderList
    });
  },
  // 批量发货--发货
  async handleSureBatch(){
    if(this.loading) return
    this.loading = true
    let { selectdIdArr } = this.data
    let res = await request.get(api.batchVerification, {
      ids: selectdIdArr.join(','),
    }, { loading: false })
    if(res.httpStatus === 200 ){
      if(res.data){
        jump('coinCertificateShipping',{
          id: selectdIdArr.join(','),
          ids: selectdIdArr,
          isBatchShipment: true,
          orderNum: selectdIdArr.length
        })
      }else{
        wx.showToast({
          title: '有订单发货状态已更改',
          icon: 'none'
        })
        this.setData({
          page: 1,
          isDone: false,
        })
        this.getList(1)
      }
      this.loading = false
    }else{
      this.setData({
        page: 1,
        isDone: false,
      })
      this.getList(1)
      this.loading = false
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      isDone: false,
    })
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { page, total } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    } else {
      this.setData({
        isDone: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})